package com.appcheck.recording;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import com.appcheck.recording.model.IdNamePair;
import com.appcheck.recording.utils.Logger;

import android.view.View;
import android.widget.AdapterView;

public class PreViewManager implements Cloneable{
	/**
	 * 记录列表中的View的Name和Id,由于stack使用的是同步方法，而且这个list装的东西也不多，这里使用list替代
	 */
	private List<IdNamePair> idNameList;
	
	/**
	 * 记录列表中第一个viewGroup(除adapterView)在adpaterView中的位置
	 */
	private int indexInAdapterView = -1;

	/**
	 * 初始化,直接添加第一个adapterView
	 * @param paramView
	 */
	public PreViewManager(AdapterView paramView){
		idNameList = new ArrayList<IdNamePair>();
		IdNamePair adapterIdName = new IdNamePair(WidgetHelper.getResourceName(paramView), WidgetHelper.getResourceId(paramView));
		idNameList.add(adapterIdName);
	}
	
	@Override
	public String toString() {
		if(idNameList != null){
			String str = "";
			for(IdNamePair pair: idNameList){
				str += (pair.toString() + ",");
			}
			str += "indexInAdapterView = " + indexInAdapterView; 
			return str;
		}else{
			return null;
		}
	}
	
	public void pushViewIdName(View paramView){
		IdNamePair pair = new IdNamePair(WidgetHelper.getResourceName(paramView), WidgetHelper.getResourceId(paramView));
		this.idNameList.add(pair);
	}
	
	public void popViewIdName(){
		this.idNameList.remove(idNameList.size() - 1);
	}
	
	public final List<IdNamePair> getIdNameList() {
		return idNameList;
	}

	public final void setIdNameList(Stack<IdNamePair> idNameStack) {
		idNameStack = idNameStack;
	}

	public final int getIndexInAdapterView() {
		return indexInAdapterView;
	}
	
	public final void setIndexInAdapterView(int indexInAdapterView) {
		this.indexInAdapterView = indexInAdapterView;
	}
	
	public final void setIndexInAdapterView(View paramView){
		this.indexInAdapterView = WidgetHelper.getIndexInAdapterViewOfView(paramView);
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		PreViewManager newPreViewManager = (PreViewManager)super.clone();
		newPreViewManager.idNameList = new Stack<IdNamePair>();
		for(IdNamePair pair : idNameList){
			newPreViewManager.idNameList.add(pair);
		}
		return newPreViewManager;
	}
	
}
